<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <ContentWrap>
      <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
        <el-form-item label="计划编号" prop="planNo">
          <el-input
            v-model="queryParams.planNo"
            placeholder="请输入计划编号"
            :clearable="true"
            @keyup.enter="getPage"
          />
        </el-form-item>
        <el-form-item label="计划名称" prop="planName">
          <el-input
            v-model="queryParams.planName"
            placeholder="请输入计划名称"
            :clearable="true"
            @keyup.enter="getPage"
          />
        </el-form-item>
        <el-form-item label="计划状态" prop="planStatus">
          <el-select
            v-model="queryParams.planStatus"
            placeholder="请输入计划状态"
            :clearable="true"
          >
            <el-option
              v-for="dict in getIntDictOptions(DICT_TYPE.RESTAURANT_PLAN_STATUS)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="餐饮套餐" prop="comboId">
          <el-select v-model="queryParams.comboId" placeholder="请输入餐饮套餐" :clearable="true">
            <el-option
              v-for="dict in comboList"
              :key="dict.id"
              :label="dict.comboName"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleClickSearch">
            <Icon icon="ep:search" />
            搜索
          </el-button>
          <el-button @click="handleClickReset">
            <Icon icon="ep:refresh" />
            重置
          </el-button>
        </el-form-item>
      </el-form>
      <Table
        :columns="columns"
        :page-param="queryParams"
        :adaptive="true"
        :page-data="queryData"
        save-key="RestaurantPlanSelect"
        @page-change="getPage"
        @row-dblclick="handleTableDblclick"
      />
    </ContentWrap>
  </Dialog>
</template>
<script setup name="RestaurantPlanSelect" lang="ts">
import * as RestaurantPlanApi from '@/api/restaurant/plan'
import * as RestaurantRecipeApi from '@/api/restaurant/recipe'
import { DICT_TYPE, getDictObj, getIntDictOptions } from '@/utils/dict'
import { formatDate } from '@/utils/formatTime'
const dialogTitle = ref('')
const dialogVisible = ref(false)
const queryFormRef = ref()
const comboList = ref()
const queryParams = ref({
  planNo: undefined,
  planName: undefined,
  planStatus: undefined,
  comboId: undefined
})
const queryData = ref()
const columns = [
  {
    label: '计划编号',
    prop: 'planNo'
  },
  {
    label: '餐饮计划名称',
    prop: 'planName'
  },
  {
    label: '开始时间',
    prop: 'beginTime',
    formatter: ({ beginTime }) => formatDate(beginTime, 'YYYY-MM-DD')
  },
  {
    label: '结束时间',
    prop: 'endTime',
    formatter: ({ endTime }) => formatDate(endTime, 'YYYY-MM-DD')
  },
  {
    label: '计划状态',
    prop: 'planStatus',
    formatter: ({ planStatus }) => getDictObj(DICT_TYPE.RESTAURANT_PLAN_STATUS, planStatus).label
  },
  {
    label: '套餐',
    prop: 'comboId',
    formatter: ({ comboId }) => comboList.value.find((obj) => obj.id === comboId).comboName
  },
  {
    label: '创建时间',
    prop: 'createTime'
  }
]
const emit = defineEmits(['selectValue'])
/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
  comboList.value = await RestaurantRecipeApi.listQueryCombo()
  getPage()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 搜索按钮操作 */
const handleClickSearch = () => {
  getPage()
}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 处理响应事件  */
const getPage = async () => {
  queryData.value = await RestaurantPlanApi.query(queryParams.value)
}

/** 双击table列操作  */
const handleTableDblclick = (row) => {
  emit('selectValue', row)
  close()
}

/** 关闭弹窗 */
const close = async () => {
  dialogVisible.value = false
}
</script>
